package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.GraduateProjectCacheDao;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProject;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class GraduateProjectCacheService {
    private GraduateProjectCacheDao graduateProjectCacheDao= GraduateProjectCacheDao.getInstance();
    private static GraduateProjectCacheService graduateProjectCacheService = new GraduateProjectCacheService();

    private GraduateProjectCacheService(){}

    public static GraduateProjectCacheService getInstance(){
        return graduateProjectCacheService;
    }


    public Collection<GraduateProject> findByTeacherId
            (int teacherId) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<GraduateProject> graduateProjects =
                graduateProjectCacheDao.findByTeacherId(teacherId,connection) ;
        JdbcHelper.close(connection);
        return graduateProjects;
    }

    public boolean add(GraduateProject graduateProject) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean added =false;
        try {
            //增加用户
            added = graduateProjectCacheDao.add(graduateProject,connection);
            //提交事务
            connection.commit();
        }catch (Exception e){
            //回滚
            connection.rollback();

        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added ;
    }
    public boolean update(GraduateProject graduateProject) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean updated =false;
        try {
            //增加用户
            updated = graduateProjectCacheDao.update(graduateProject,connection);
            //提交事务
            connection.commit();

        }catch (Exception e){
            //回滚
            connection.rollback();
            e.printStackTrace();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return updated ;
    }

    public boolean delete(Integer id) throws SQLException{
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean deleted =false;
        try {
            //增加用户
            deleted  = graduateProjectCacheDao.delete(id,connection);
            //提交事务
            connection.commit();

        }catch (Exception e){
            //回滚
            connection.rollback();

        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted  ;
    }
    public boolean deletes(List<GraduateProject> listToDelete) throws SQLException{
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean deleted =false;
        try {
            for(GraduateProject e:listToDelete) {
                int id= e.getId();
                //删除用户
                deleted = graduateProjectCacheDao.delete(id, connection);
            }
                //提交事务
                connection.commit();
        }catch (Exception e){
            //回滚
            connection.rollback();

        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted  ;
    }
}